package com.sky.module.system.utils;

import com.sky.module.system.Department.domain.entity.Department;
import com.sky.module.system.Department.domain.vo.DepartmentQueryVO;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/***
 * @description 生成部门树列表
 * @param
 * @return
 * @date 2023/2/21 14:50
 *
 */
public class DepartmentTree {
    public static List<Department> makeDepartmentTree(List<Department> departmentList, Long pid) {
        List<Department> list = new ArrayList<>();

        Optional.ofNullable(departmentList).orElse(new ArrayList<>())
                .stream().filter(item -> item != null && item.getPid() == pid).forEach(e -> {

            DepartmentQueryVO departmentQueryVO = new DepartmentQueryVO();
            BeanUtils.copyProperties(e, departmentQueryVO);
            List<Department> children = makeDepartmentTree(departmentList, e.getId());
            departmentQueryVO.setChildren(children);
            list.add(departmentQueryVO);
        });
        return list;
    }
}
